﻿using System;
using System.Net;
using System.Windows.Input;
using System.Windows.Threading;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using SilverlightWebcamEncoder.Helpers;
using SilverlightWebcamEncoder.Messages;

namespace SilverlightWebcamEncoder.ViewModels
{
    public class YouTubeUploadViewModel : ViewModelBase
    {
        private string m_username;
        private string m_password;
        private string m_videoTitle;
        private string m_uploadMessage;
        private bool m_isIdle = true;

        private YouTubeFileUpload m_youtubeUploader;
        private DispatcherTimer m_uploadTimer;

        public YouTubeUploadViewModel()
        {
            UploadVideo = new RelayCommand(OnUploadVideo);

            VideoTitle = "Mix10";
            m_youtubeUploader = new YouTubeFileUpload(App.Injector.PayloadExtractionPath);
            m_uploadTimer = new DispatcherTimer();
            m_uploadTimer.Interval = TimeSpan.FromSeconds(1);
            m_uploadTimer.Tick += UploadTimer_Tick;
        }

        private void UploadTimer_Tick(object sender, EventArgs e)
        {
            if(m_youtubeUploader.Completed)
            {
                if(m_youtubeUploader.Failed)
                    UploadMessage = "Upload Failed";
                else
                    UploadMessage = "Upload Completed";

                IsIdle = true;

                m_uploadTimer.Stop();
            }
        }

        private void OnUploadVideo()
        {
            var request = new LocalVideoFilenameRequest();
            Messenger.Default.Send(request);

            string filename = request.FilePath;

            if (string.IsNullOrEmpty(filename))
                return;

            m_uploadTimer.Start();

            UploadMessage = "Uploading...";

            IsIdle = false;
            m_youtubeUploader.Upload(Username, Password, VideoTitle, filename);
        }

        public ICommand UploadVideo { get; private set; }

        public bool IsIdle
        {
            get { return m_isIdle; }
            set
            {
                m_isIdle = value;
                RaisePropertyChanged("IsIdle");
            }
        }

        public string UploadMessage
        {
            get { return m_uploadMessage; }
            set
            {
                m_uploadMessage = value;
                RaisePropertyChanged("UploadMessage");
            }
        }

        public string Username
        {
            get { return m_username; }
            set
            {
                m_username = value;
                RaisePropertyChanged("Username");
            }
        }

        public string Password
        {
            get { return m_password; }
            set
            {
                m_password = value;
                RaisePropertyChanged("Password");
            }
        }

        public string VideoTitle
        {
            get
            {
                return m_videoTitle;
            }
            set
            {
                m_videoTitle = value;
                RaisePropertyChanged("VideoTitle");
            }
        }
    }
}
